package com.cx.controller;

import com.cx.common.ApiReturn;
import com.cx.pojo.Gearbox;
import com.cx.service.GearboxService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("gearbox")
@CrossOrigin//跨域
public class GearboxController {
    @Autowired
    GearboxService gearboxService;

    //添加变速箱
    @RequestMapping("insertGearbox")
    @ResponseBody
    public ApiReturn insertGearbox(Gearbox gearbox){
        int rows = gearboxService.insert(gearbox);
        if (rows<1){
            return ApiReturn.error("添加失败");
        }
        return ApiReturn.success("添加成功");
    }


    //修改变速箱
    @RequestMapping("updateGearbox")
    @ResponseBody
    public ApiReturn updateGearbox(Gearbox gearbox){
        int rows = gearboxService.updateByPrimaryKey(gearbox);
        if (rows<1){
            return ApiReturn.error("修改失败");
        }
        return ApiReturn.success("修改成功");
    }

    //删除变速箱
    @RequestMapping("deleteGearbox")
    @ResponseBody
    public ApiReturn deleteGearbox(Integer gId) {
        int rows = gearboxService.deleteByPrimaryKey(gId);
        if (rows < 1) {
            return ApiReturn.error("删除失败");
        }
        return ApiReturn.success("删除成功");
    }

    //根据id查询变速箱
    @RequestMapping("selectGearboxById")
    @ResponseBody
    public ApiReturn selectGearboxById(Integer gId) {
        Gearbox gearboxInfo = gearboxService.selectByPrimaryKey(gId);
        if (gearboxInfo != null) {
            return ApiReturn.success("变速箱数据详情",gearboxInfo);
        }
        return ApiReturn.error("未获取该变速箱数据详情");
    }

    //查询所有变速箱
    @RequestMapping("selectAllGearbox")
    @ResponseBody
    public ApiReturn selectAllGearbox(Gearbox gearbox) {
        PageInfo<Gearbox> page = gearboxService.selectAllGearbox(gearbox);
        if (page.getList() != null) {
            return ApiReturn.success("所有变速箱数据",page);
        }
        return ApiReturn.error("无数据");
    }
}
